﻿//This file contains code currently not accessed by the interpreter
//Transfer flow from function "main" to class iCommands to introduce more controls
//Than opening files to interpret


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EjotyI
{
    //interpreter commands and functions
    class iCommands
    {
        Computer comp = new Computer();     //declare object refrence
        public void controlCmd(string input)
        {
            string[] order = input.Split(' ');
            switch (order[0])
            {
                //command to open file and run it
                case "/run":
                    try
                    {
                        run(order[1]);
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(ex.ToString());
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    break;
                case "/read":
                    try
                    {
                        read(order[1]);
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(ex.ToString());
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    break;
                case "/help":
                    help();
                    break;
                case "help":
                    help();
                    break;
                default:
                    break;
            }
        }
        private void run(string file)
        {
            //declarations
            StreamReader myStr;     //the IO tool
            string text;        //to hold the entire file's contents
            if (File.Exists(file))       //check to make sure file exists
            {
                Console.WriteLine("File Found");    //tell user that the file exists
                myStr = File.OpenText(file);        //open access to the file
                text = myStr.ReadToEnd();       //read the contents of the file and store in text
                char[] delimiters = { ' ', '|', '\t' };              //seperate the indivdual commands using " ", "|", "\t"
                comp.input = text.Split(delimiters);     //split text into individual commands, store in input
                //time to begin the actual interpretation
                //first generate the tokens
                comp.tokens();
                //now a loop to execute the file to the end
                for (; comp.a < comp.myToken.tokens.Length; comp.a++)
                {
                    comp.intertok(comp.myToken.tokens[comp.a]);
                }
            }
            else
            {
                Exception ex = new Exception("Could not find file.");
                throw ex;
            }
        }
        private void read(string file)
        {
           //declarations
           StreamReader myStr;     //the IO tool
           string text;        //to hold the entire file's contents
           string[] input;      //holds seperated file contents
           if (File.Exists(file))
           {
               Console.WriteLine("File Found");    //tell user that the file exists
               Console.WriteLine();
               myStr = File.OpenText(file);     //open access to the file
               text = myStr.ReadToEnd();           //read the file
               char delimiter = '\n';              //seperate the indivdual commands using "/"
               input = text.Split(delimiter);     //split text into individual commands, store in input
               for (int a = 0; a < input.Length; a++)
               {
                   Console.WriteLine(input[a]);
               }
           }
           else
           {
               Exception ex = new Exception("Could not find file.");
               throw ex;
           }
        }
        private void help()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("The following are interpreter commands:");
            Console.WriteLine("/run <file_name> \t interprets and runs file at given location");
            Console.WriteLine("/read <file_name> \t displays file's contents on the interpreter CLI");
            Console.WriteLine("/help or help \t displays list of commands");
            Console.ForegroundColor = ConsoleColor.Gray;
        }
    }
}
